import 'package:flutter/material.dart';

/// 自定义输入框组件
class CustomTextField extends StatefulWidget {
  final TextEditingController? controller;
  final String placeholder;
  final Color backgroundColor;
  final double borderRadius;
  final Widget? leftIcon;
  final VoidCallback? onLeftIconPressed;
  final Widget? rightIcon;
  final VoidCallback? onRightIconPressed;
  final TextInputType keyboardType;
  final bool obscureText;
  final ValueChanged<String>? onChanged;
  final bool enabled;
  final double height;
  final EdgeInsetsGeometry padding;
  final TextStyle? placeholderStyle;
  final TextStyle? textStyle;

  const CustomTextField({
    Key? key,
    this.controller,
    this.placeholder = '',
    this.backgroundColor = const Color(0xFFF5F5F5),
    this.borderRadius = 8.0,
    this.leftIcon,
    this.onLeftIconPressed,
    this.rightIcon,
    this.onRightIconPressed,
    this.keyboardType = TextInputType.text,
    this.obscureText = false,
    this.onChanged,
    this.enabled = true,
    this.height = 56.0,
    this.padding = const EdgeInsets.symmetric(horizontal: 16),
    this.placeholderStyle,
    this.textStyle,
  }) : super(key: key);

  @override
  _CustomTextFieldState createState() => _CustomTextFieldState();
}

class _CustomTextFieldState extends State<CustomTextField> {
  late TextEditingController _controller;

  @override
  void initState() {
    super.initState();
    _controller = widget.controller ?? TextEditingController();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: widget.height,
      decoration: BoxDecoration(
        color: widget.backgroundColor,
        borderRadius: BorderRadius.circular(widget.borderRadius),
      ),
      child: Row(
        children: [
          // 左侧图标
          if (widget.leftIcon != null)
            _buildIconButton(
              icon: widget.leftIcon!,
              onPressed: widget.onLeftIconPressed,
            ),

          // 输入框主体
          Expanded(
            child: Padding(
              padding: widget.padding,
              child: TextField(
                controller: _controller,
                keyboardType: widget.keyboardType,
                obscureText: widget.obscureText,
                onChanged: widget.onChanged,
                enabled: widget.enabled,
                style:
                    widget.textStyle ??
                    TextStyle(fontSize: 16, color: Colors.black),
                decoration: InputDecoration(
                  border: InputBorder.none,
                  hintText: widget.placeholder,
                  hintStyle:
                      widget.placeholderStyle ??
                      TextStyle(fontSize: 16, color: Color(0xFF999999)),
                ),
              ),
            ),
          ),

          // 右侧图标
          if (widget.rightIcon != null)
            _buildIconButton(
              icon: widget.rightIcon!,
              onPressed: widget.onRightIconPressed,
            ),
        ],
      ),
    );
  }

  Widget _buildIconButton({required Widget icon, VoidCallback? onPressed}) {
    return GestureDetector(
      onTap: onPressed,
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 12),
        child: IconTheme(
          data: IconThemeData(color: Color(0xFF666666), size: 24),
          child: icon,
        ),
      ),
    );
  }

  @override
  void dispose() {
    if (widget.controller == null) {
      _controller.dispose();
    }
    super.dispose();
  }
}
